#!/usr/bin/env bash
# MainSail installation script
# Installs MainSail and nginx
# Written by Raymond Himle
# Thanks to meteyou
# Revamped by KwadFan
# GPL V3
########


# Source error handling, leave this in place
set -xe

# Source CustomPIOS common.sh
source /common.sh
install_cleanup_trap

### Install mainsail.cfg
unpack /filesystem/home/${BASE_USER} /home/${BASE_USER} ${BASE_USER}

echo_green "Installing Mainsail Webfrontend ..."

### install all deps at once for time consumption reasons.
## APT: Update Repo Database and install Dependencies

apt update && apt install ${MAINSAIL_DEPS} -y

### Preparing nginx
rm /etc/nginx/sites-enabled/default
ln -s /etc/nginx/sites-available/mainsail /etc/nginx/sites-enabled/

# lower nginx rotate cycle to 2 instead 14
sudo sed -i 's/rotate 14/rotate 2/' /etc/logrotate.d/nginx

### Download and Install Mainsail Web Frontend
pushd /home/${BASE_USER}
sudo -u ${BASE_USER} wget -q --show-progress -O mainsail.zip "${MAINSAIL_URL}"
sudo -u ${BASE_USER} unzip mainsail.zip -d /home/${BASE_USER}/mainsail
## cleanup
rm /home/${BASE_USER}/mainsail.zip
popd

### Link logfiles to klipper_logs
ln -s /var/log/nginx/mainsail-access.log /home/${BASE_USER}/klipper_logs/
ln -s /var/log/nginx/mainsail-error.log /home/${BASE_USER}/klipper_logs/

# Unpack root at the end, so files are modified before
unpack /filesystem/root /